package org.xutils.main;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.zzh.lib.core.utils.HDateUtils;
import com.zzh.lib.core.utils.HTaskUtils;
import com.zzh.lib.core.utils.LogUtils;

import net.sqlcipher.Cursor;
import net.sqlcipher.database.SQLiteDatabase;

import org.xutils.db.DbManager;
import org.xutils.db.ex.DbException;
import org.xutils.db.model.DDLTableModel;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Random;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onClickAddWANView(View view) {
        DbManager dbManager = App.getInstance().getDbManager();

        UserModel model = new UserModel();

        try {
            model.age = 13;
            model.setExtra("额外数据: " + System.currentTimeMillis());
            model.setCreateTime(System.currentTimeMillis());
            model.setUserName(HDateUtils.format(System.currentTimeMillis(), HDateUtils.DF_YYYY_MM_DD_HH_MM_SS));
            model.setPassword("123123");
            model.setUnread(1);
            model.setTargetId(String.valueOf(System.currentTimeMillis()));
            org.xutils.db.log.Log.getInstance().e("插入操作");
            dbManager.saveOrUpdate(model);
        } catch (DbException e) {
            e.printStackTrace();
        }


    }

    public void onClickTableDDL(View view) {
        DbManager dbManager = App.getInstance().getDbManager();
        try {
            Optional<List<DDLTableModel>> allTables = dbManager.getAllTables();
            if (allTables.isPresent()) {
                List<DDLTableModel> ddlTableModels = allTables.get();
                for (DDLTableModel dtm :
                        ddlTableModels) {
                    LogUtils.e(dtm.getType() + ", " + dtm.getName() + ", " + dtm.getTbl_name() + ", " + dtm.getRootpage());
                    LogUtils.e(dtm.getSql());
                    LogUtils.e("----------------------------------------------------------------------------------------------------");
                }
            }
        } catch (DbException e) {
            throw new RuntimeException(e);
        }
    }

    public void onClickTableDDLSingle(View view) {
        DbManager dbManager = App.getInstance().getDbManager();
        try {
            Optional<DDLTableModel> allTables = dbManager.getTable("table", "cim_user");
            if (allTables.isPresent()) {
                DDLTableModel dtm = allTables.get();
                LogUtils.e(dtm.getType() + ", " + dtm.getName() + ", " + dtm.getTbl_name() + ", " + dtm.getRootpage());
                LogUtils.e(dtm.getSql());
                LogUtils.e("----------------------------------------------------------------------------------------------------");
            }
        } catch (DbException e) {
            throw new RuntimeException(e);
        }
    }

    public void onClickFindView(View view) {
        DbManager dbManager = App.getInstance().getDbManager();
        try {
            List<CIMUserModel> all = dbManager.findAll(CIMUserModel.class);
            if (all != null && !all.isEmpty()) {
                for (CIMUserModel model : all) {
                    Log.d("-----", model.getId() + ", " + model.getUserName() + ", " + model.getPassword());
                }
            }


            List<UserModel> allUM = dbManager.findAll(UserModel.class);
            if (allUM != null && !allUM.isEmpty()) {
                for (UserModel model : allUM) {
                    Log.d("-----2", model.toString());
                }
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }


    public void onClickAddView(View view) {
        Random ra = new Random();
        String[] targetIds = {"1000", "2000", "3000", "4000", "5000"};
        for (int i = 0; i < 20; i++) {
            HTaskUtils.getSingleInstance().execute(() -> {
                try {
                    UserModel um = new UserModel();
                    um.createTime = System.currentTimeMillis();
                    um.age = ra.nextInt(3);
                    um.targetId = targetIds[um.age];
                    um.password = "互联网医院审核结果通知";
                    um.userName = "测试： " + um.age;
                    um.extra = "互联网医院";
                    if (TextUtils.equals("1000", um.targetId)) {
                        CIMUserModel cimUserModel = um.copyUM();
                        cimUserModel.id = System.currentTimeMillis();
                        cimUserModel.userName = um.userName;
                        saveOrUpdateCUM(cimUserModel);
                        um.id = "1000";
                        um.userName = "系统消息";
                    } else if (TextUtils.equals("2000", um.targetId)) {
                        um.id = String.valueOf(2000);
                    } else {
                        um.id = "3000";
                    }
                    saveOrUpdate(um);
                } catch (DbException e) {
                    e.printStackTrace();
                }
            });
        }
    }

    void saveOrUpdate(UserModel um) throws DbException {
        List<UserModel> all = App.getInstance().getDbManager().selector(UserModel.class).where("target_id", "=", um.targetId).findAll();
        if (all != null && !all.isEmpty()) {
            UserModel userModel = all.get(0);
            um.id = userModel.id;
            App.getInstance().getDbManager().update(um);
        } else {
            App.getInstance().getDbManager().save(um);
        }
    }

    void saveOrUpdateCUM(CIMUserModel um) throws DbException {
        List<CIMUserModel> all = App.getInstance().getDbManager().selector(CIMUserModel.class).where("target_id", "=", um.targetId).findAll();
        if (all != null && !all.isEmpty()) {
            CIMUserModel userModel = all.get(0);
            um.id = userModel.id;
            App.getInstance().getDbManager().update(um);
        } else {
            App.getInstance().getDbManager().save(um);
        }
    }


    public void onClickAddOneView(View view) {
        String[] targetIds = {"1000", "2000", "3000", "4000", "5000"};
        HTaskUtils.getSingleInstance().execute(() -> {
            try {
                UserModel um = new UserModel();
                um.createTime = System.currentTimeMillis();
                um.age = 0;
                um.targetId = targetIds[um.age];
                um.password = "互联网医院审核结果通知";
                um.userName = "测试： " + um.age;
                um.extra = "互联网医院审核结果通知互联网医院审核结果通知互联网医院审核结果通知";
                if (TextUtils.equals("1000", um.targetId)) {
                    CIMUserModel cimUserModel = um.copyUM();
                    cimUserModel.id = System.currentTimeMillis();
                    cimUserModel.userName = um.userName;
                    App.getInstance().getDbManager().saveOrUpdate(cimUserModel);
                    um.id = "1000";
                    um.userName = "系统消息";
                } else if (TextUtils.equals("2000", um.targetId)) {
                    um.id = "2000";
                } else {
                    um.id = "3000";
                }
                App.getInstance().getDbManager().saveOrUpdate(um);
            } catch (DbException e) {
                e.printStackTrace();
            }
        });
    }

    public void onClickDeleteView(View view) {
        DbManager dbManager = App.getInstance().getDbManager();
        try {
            List<UserModel> all = dbManager.findAll(UserModel.class);
            if (all != null && !all.isEmpty()) {
                UserModel userModel = all.get(0);
                dbManager.delete(userModel);
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    public void onClickUpdateView(View view) {
        DbManager dbManager = App.getInstance().getDbManager();
        try {
            List<UserModel> all = dbManager.findAll(UserModel.class);
            if (all != null && !all.isEmpty()) {
                UserModel userModel = all.get(0);
                userModel.setUserName("-----测试修改:" + System.currentTimeMillis());
                dbManager.saveOrUpdate(userModel);
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }
}